package 可重入锁.lock的不同方式.普通的lock;

import java.util.concurrent.locks.ReentrantLock;

public class Lockk implements Runnable{
    private ReentrantLock lock = new ReentrantLock();// 非公平锁
    private static int i  = 0;


    @Override
    public void run() {
        for (int j = 0; j < 100000; j++) {
            lock.lock();// 不加锁，多个线程会相互覆盖掉结果
            i++;
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Lockk lockk = new Lockk();
        Thread t1 = new Thread(lockk);
        Thread t2 = new Thread(lockk);
        t1.start();
        t2.start();
        Thread.sleep(4000);// 让线程执行完for循环，再看变量i
        System.out.println(lockk.i);// 200000
    }
}
